/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ClassUtil;
import cn.star.framework.core.Constant;
import cn.star.framework.core.api.AppResult;
import cn.star.framework.core.dict.EnumEntry;
import cn.star.framework.util.EnumEntryHelper;
import cn.star.framework.util.SpringContextUtil;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 框架扩展Api<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/extend/api")
@SuppressWarnings("rawtypes")
public class ExtendApiController {

  @GetMapping(value = "/container/beans")
  @ApiOperation(value = "获取容器中的实例名称")
  public AppResult<String[]> getContainerBeans() {
    try {
      String[] names = SpringContextUtil.getApplicationContext().getBeanDefinitionNames();
      return AppResult.ofSuccess(names);
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }

  @GetMapping(value = "/entry/enums")
  @ApiOperation(value = "获取框架内的静态字典，继承自{@link EntryEnum<T>}的枚举")
  public AppResult getEntryEnums() {
    try {
      Set<Class<?>> classes = ClassUtil.scanPackageBySuper(Constant.BASE_PACKAGE, EnumEntry.class);
      if (CollectionUtil.isEmpty(classes)) {
        return AppResult.ofSuccess();
      }
      List<Map<String, Object>> list = new ArrayList<>(classes.size());
      for (Class<?> clazz : classes) {
        if (clazz.isEnum()) {
          try {
            List<EnumEntry<?>> entries = EnumEntryHelper.get(clazz).getValues();
            Map<String, Object> m = new HashMap<>(1);
            List<Map<?, String>> v = new ArrayList<>();
            m.put(
                clazz.getName(),
                entries.stream()
                    .map(
                        entry -> {
                          Map<Object, String> map = new HashMap<>(1);
                          map.put(entry.getValue(), entry.getLabel());
                          return map;
                        })
                    .collect(Collectors.toList()));
            list.add(m);
          } catch (Exception e) {
            log.warn(e.getMessage());
          }
        }
      }
      return AppResult.ofSuccess(list);
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }

  @GetMapping(value = "/dynamic/query")
  @ApiOperation(value = "动态查询关系型数据库数据")
  public AppResult dynamicQuery(HttpServletRequest request) {
    try {
      return AppResult.ofSuccess();
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }

}
